#include "attributeset.h"
#include "biosagelabel.h"

//---------------------------------ATTRIBUTE SET---------------------------------//

QualAttributeSet::QualAttributeSet(QString n, QList<QString>& dataElts, QList<QString>& attElts, QGraphicsItem *parent) :
    name(n), QGraphicsWidget(parent)
{
    if( dataElts.size() != attElts.size() )
    {
        qDebug() << "size mismatch between data array and att array in qual att set constructor" << endl;
        return;
    }

    //Setup the layout that will hold all the labels
    layout = new QGraphicsLinearLayout( Qt::Vertical );
    //BioSageLabel* l = new BioSageLabel( name, QColor(Qt::white), this);
    attributeLabels.push_back( new BioSageLabel( name, QColor(Qt::white), this) );
    //layout->addItem( attributeLabels[0] );

//    p = new QPushButton( name );
//    pw = new QGraphicsProxyWidget(this, Qt::Widget);//proxy table for graphics view architecture
//    pw->setWidget( p );
//    pw->setPos(0,0);

    //loop through data elts and attribute elts and fill attribute, attributeDomain, and color arrays
   QListIterator<QString> dataItr( dataElts );
   QListIterator<QString> attItr( attElts );
   while( dataItr.hasNext() && attItr.hasNext() )
   {
        QString dataVal = dataItr.next();
        QString attVal = attItr.next();
        QualAttribute* q = new QualAttribute( dataVal, attVal, this);
        attributes.push_back(q);
        q->setVisible( false );

        qDebug() << "adding attribute to set: " << dataVal << " " << attVal << " " << endl;

        //see if this attribute has already been encountered
        bool alreadyEncountered = false;
        for(int i = 0; i < attributeDomain.size(); i++)
        {
            if( attributeDomain[i]->getAttributeValue() == attributes[ attributes.size()-1 ]->getAttributeValue() )
            {
                alreadyEncountered = true;
                attributes[ attributes.size()-1 ]->setColor( attributeDomain[i]->getColor() );//set att color to appr color
            }
        }
        //if it has not been encountered, add it to domain
        if( !alreadyEncountered )
        {
            QualAttribute* dq = new QualAttribute( dataVal, attVal, this);//could also use copy constructor
            attributeDomain.push_back( dq  );
            dq->setVisible( true );

            //this will not work forever-- need mechanism for assigning colors!!!
            QColor c;
            if( attributeDomain.size() == 1 )
            {
                c.setNamedColor( "lightskyblue" );
            } else if( attributeDomain.size() == 2 )
            {
                c.setNamedColor( "lightseagreen");
            } else if( attributeDomain.size() == 3 )
            {
                c.setNamedColor( "mediumpurple");
            }
            dq->setColor( c );
            attributes[ attributes.size()-1]->setColor( c );
        }
   }

   qDebug() << "attribute domain = " << endl;
   for(int i = 0; i < attributeDomain.size(); i++)
   {
       qDebug() << *(attributeDomain[i]);

   }
   qDebug() << endl;

   //this->setLayout( layout );

   update();
}

QualAttributeSet::QualAttributeSet( const QualAttributeSet & other ) :
    name(other.name), QGraphicsWidget(other.parentItem())
{
    //copy attribute values
    for( int i = 0; i < other.attributes.size(); i++ )
    {
        QualAttribute* q = new QualAttribute(*(other.attributes[i]));
        attributes.push_back(q);
    }
    //copy domain
    for( int i = 0; i < other.attributeDomain.size(); i++)
    {
        QualAttribute* q = new QualAttribute(*(other.attributes[i]));
        attributeDomain.push_back(q);
    }
    //copy color
    for( int i = 0; i < other.attributeColors.size(); i++)
    {
        attributeColors.push_back(other.attributeColors[i]);
    }
    //copy attributelabels
    attributeLabels.clear();
    for( int i = 0; i < other.attributeLabels.size(); i++)
    {
        attributeLabels.push_back(new BioSageLabel( * (other.attributeLabels[i]) ) );
        attributeLabels[i]->setParentItem(this);
        attributeLabels[i]->setVisible(true);
    }

    update();
}


QPointer<QualAttribute> QualAttributeSet::getAttribute(int idx) const
{
    return attributes[idx];
}

int QualAttributeSet::getNumberOfAttributes() const
{
    return attributes.size();
}

QualAttributeSet& QualAttributeSet::operator=(const QualAttributeSet &rhs)
{
    if (this == &rhs)      // Same object?
          return *this;        // Yes, so skip assignment, and just return *this.

   // Deallocate, allocate new space, copy values...

   return *this;
}

QRectF QualAttributeSet::boundingRect() const
{
    return QRectF(-100, -100, 200, 200); //note- before attributes added, was width = 700
}

QPainterPath QualAttributeSet::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}

void QualAttributeSet::paint(QPainter *painter, const QStyleOptionGraphicsItem * item, QWidget * widget)
{
//    qDebug() << "attribute set has " << attributeLabels.size() << " labels to display" << endl;
    QColor background(5, 5, 5, 255 );
    painter->setBrush(background);

    QRectF bRect = boundingRect();
    bRect.adjust(1, 1, -1, -1);
    painter->drawRect(bRect);

//    QFont f;
//    f.setPointSize(10);
//    painter->setFont(f);
//    painter->setPen(QPen(Qt::white) ); //Qt::black) );//Qt::lightGray));
//    painter->drawText(boundingRect(), Qt::AlignCenter, name, &boundingRect());
//    for( int i = 0 ; i < attributeDomain.size(); i++ )
//    {
//        toShow.append( attributeDomain[i]->attributeValue );
//    }

//    qDebug() << attributeProxyLabels.size() << endl;
       //attributeProxyLabels[0]->paint(painter, item, widget);


    //I don't know why I have to do this but it is REALLY frustrating
   for(int i =0; i < attributeLabels.size(); i++)
    {
        attributeLabels[i]->setParent(this);
        attributeLabels[i]->setVisible(true);
        update();
//        attributeLabels[i]->paint(painter, item, widget);
    }

//   pw->paint(painter, item, widget);

//   QList<QGraphicsItem *> ci = this->childItems();
//   qDebug() << "num child items = " << ci.size() << endl;


    //divide space by number of items in the domain
}

QDebug operator <<( QDebug out, const QualAttributeSet & q )
{
    for(int i = 0; i < q.attributes.size(); i++)
        out << *(q.attributes[i]) << "; ";
    out << endl;
    return out;
}

//-------------------------------END ATTRIBUTE SET------------------------------//
//-----------------------------------ATTRIBUTE----------------------------------//
QualAttribute::QualAttribute(QString& dataVal, QString& attVal, QGraphicsItem *parent) :
    dataValue(dataVal), attributeValue(attVal), QGraphicsObject(parent)
{

}

QualAttribute::QualAttribute(const QualAttribute & other ) :
    dataValue(other.getDataValue()), attributeValue(other.getDataValue()), color( other.getColor()), QGraphicsObject(other.parentItem())
{

}

QRectF QualAttribute::boundingRect() const
{
    return QRectF(-20, -20, 40, 40); //note- before attributes added, was width = 700
}

QPainterPath QualAttribute::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}

void QualAttribute::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
//    QColor background(10, 10, 10, 255 );
//    painter->setBrush(background);

//    QRectF bRect = boundingRect();
//    bRect.adjust(1, 1, -1, -1);
//    painter->drawRect(bRect);
}



QColor QualAttribute::getColor() const
{
    return color;
}

void QualAttribute::setColor(const QColor& newColor)
{
    color = newColor;
}

QString QualAttribute::getAttributeValue() const
{
    return attributeValue;
}

void QualAttribute::setAttributeValue(const QString& newValue)
{
    attributeValue = newValue;
}

QString QualAttribute::getDataValue() const
{
    return dataValue;
}

void QualAttribute::setDataValue(const QString& newValue)
{
    dataValue = newValue;
}

QDebug operator <<( QDebug out, const QualAttribute & q )
{
    out << q.getAttributeValue() << " " << q.getDataValue() << " " << q.getColor() << endl;
    return out;
}

//---------------------------------END ATTRIBUTE--------------------------------//
//--------------------------------QUANT ATTRIBUTE------------------------------//



//-----------------------------END QUANT ATTRIBUTE------------------------------//
//------------------------------QUANL ATTRIBUTE--------------------------------//


//-----------------------------END QUANL ATTRIBUTE----------------------------//
